From 8b1b3b09114614356380e45d480a70271f9d86a9 Mon Sep 17 00:00:00 2001 From: "mjw@wray-m-3.hpl.hp.com" Date: Fri, 13 Aug 2004 13:08:23 +0000 Subject: [PATCH] bitkeeper revision 1.1159.1.52 (411cbd47vM82ZdOs6j1qiL3UlWzebA) Abstract message dispatch to methods so that more than one major message type can be handled. --- tools/python/xen/xend/server/blkif.py | 24 +++++------ tools/python/xen/xend/server/console.py | 2 +- tools/python/xen/xend/server/controller.py | 46 +++++++++++++++++----- tools/python/xen/xend/server/netif.py | 23 +++++------ 4 files changed, 56 insertions(+), 39 deletions(-) diff --git a/tools/python/xen/xend/server/blkif.py b/tools/python/xen/xend/server/blkif.py index d6d034f87c..cc49688ef5 100755 --- a/tools/python/xen/xend/server/blkif.py +++ b/tools/python/xen/xend/server/blkif.py @@ -20,12 +20,9 @@ class BlkifControllerFactory(controller.ControllerFactory): def __init__(self): controller.ControllerFactory.__init__(self) - - self.majorTypes = [ CMSG_BLKIF_BE ] - - self.subTypes = { - CMSG_BLKIF_BE_DRIVER_STATUS_CHANGED: self.recv_be_driver_status_changed, - } + self.addMethod(CMSG_BLKIF_BE, + CMSG_BLKIF_BE_DRIVER_STATUS_CHANGED, + self.recv_be_driver_status_changed) self.attached = 1 self.registerChannel() @@ -262,15 +259,12 @@ class BlkifController(controller.Controller): def __init__(self, factory, dom): controller.Controller.__init__(self, factory, dom) self.devices = {} - - self.majorTypes = [ CMSG_BLKIF_FE ] - - self.subTypes = { - CMSG_BLKIF_FE_DRIVER_STATUS_CHANGED: - self.recv_fe_driver_status_changed, - CMSG_BLKIF_FE_INTERFACE_CONNECT : - self.recv_fe_interface_connect, - } + self.addMethod(CMSG_BLKIF_FE, + CMSG_BLKIF_FE_DRIVER_STATUS_CHANGED, + self.recv_fe_driver_status_changed) + self.addMethod(CMSG_BLKIF_FE, + CMSG_BLKIF_FE_INTERFACE_CONNECT, + self.recv_fe_interface_connect) self.attached = 1 self.evtchn = None self.registerChannel() diff --git a/tools/python/xen/xend/server/console.py b/tools/python/xen/xend/server/console.py index 9221600bdb..df07528938 100755 --- a/tools/python/xen/xend/server/console.py +++ b/tools/python/xen/xend/server/console.py @@ -113,7 +113,7 @@ class ConsoleController(controller.Controller): def __init__(self, factory, dom, console_port): controller.Controller.__init__(self, factory, dom) - self.majorTypes = [ CMSG_CONSOLE ] + self.addMethod(CMSG_CONSOLE, 0, None) self.status = self.STATUS_NEW self.addr = None self.conn = None diff --git a/tools/python/xen/xend/server/controller.py b/tools/python/xen/xend/server/controller.py index e40b72cc88..b3795aeced 100755 --- a/tools/python/xen/xend/server/controller.py +++ b/tools/python/xen/xend/server/controller.py @@ -9,7 +9,7 @@ from twisted.internet import defer import channel from messages import msgTypeName, printMsg -DEBUG = 0 +DEBUG = 1 class Responder: """Handler for a response to a message with a specified id. @@ -56,9 +56,7 @@ class CtrlMsgRcvr: @ivar dom: the domain we are a control interface for @type dom: int @ivar majorTypes: major message types we are interested in - @type majorTypes: [int] - @ivar subTypes: mapping of message subtypes to methods - @ivar subTypes: {int:method} + @type majorTypes: {int:{int:method}} @ivar timeout: timeout (in seconds) for message handlers @type timeout: int @@ -72,8 +70,7 @@ class CtrlMsgRcvr: def __init__(self): self.channelFactory = channel.channelFactory() - self.majorTypes = [ ] - self.subTypes = {} + self.majorTypes = {} self.dom = None self.channel = None self.idx = None @@ -83,6 +80,37 @@ class CtrlMsgRcvr: def setTimeout(self, timeout): self.timeout = timeout + def getMethod(self, type, subtype): + """Get the method for a type and subtype. + + @param type: major message type + @param subtype: minor message type + @return: method or None + """ + method = None + subtypes = self.majorTypes.get(type) + if subtypes: + method = subtypes.get(subtype) + return method + + def addMethod(self, type, subtype, method): + """Add a method to handle a message type and subtype. + + @param type: major message type + @param subtype: minor message type + @param method: method + """ + subtypes = self.majorTypes.get(type) + if not subtypes: + subtypes = {} + self.majorTypes[type] = subtypes + subtypes[subtype] = method + + def getMajorTypes(self): + """Get the list of major message types handled. + """ + return self.majorTypes.keys() + def requestReceived(self, msg, type, subtype): """Dispatch a request message to handlers. Called by the channel for requests with one of our types. @@ -97,7 +125,7 @@ class CtrlMsgRcvr: if DEBUG: print 'requestReceived>', printMsg(msg, all=1) - method = self.subTypes.get(subtype) + method = self.getMethod(type, subtype) if method: method(msg, 1) elif DEBUG: @@ -125,7 +153,7 @@ class CtrlMsgRcvr: printMsg(msg, all=1) if self.callResponders(msg): return - method = self.subTypes.get(subtype) + method = self.getMethod(type, subtype) if method: method(msg, 0) elif DEBUG: @@ -190,7 +218,7 @@ class CtrlMsgRcvr: self.channel = self.channelFactory.domChannel(self.dom) self.idx = self.channel.getIndex() if self.majorTypes: - self.channel.registerDevice(self.majorTypes, self) + self.channel.registerDevice(self.getMajorTypes(), self) def deregisterChannel(self): """Deregister interest in our major message types with the diff --git a/tools/python/xen/xend/server/netif.py b/tools/python/xen/xend/server/netif.py index d78979c992..7a33ab8489 100755 --- a/tools/python/xen/xend/server/netif.py +++ b/tools/python/xen/xend/server/netif.py @@ -25,12 +25,9 @@ class NetifControllerFactory(controller.ControllerFactory): def __init__(self): controller.ControllerFactory.__init__(self) - - self.majorTypes = [ CMSG_NETIF_BE ] - - self.subTypes = { - CMSG_NETIF_BE_DRIVER_STATUS_CHANGED: self.recv_be_driver_status_changed, - } + self.addMethod(CMSG_NETIF_BE, + CMSG_NETIF_BE_DRIVER_STATUS_CHANGED, + self.recv_be_driver_status_changed) self.attached = 1 self.registerChannel() @@ -218,14 +215,12 @@ class NetifController(controller.Controller): controller.Controller.__init__(self, factory, dom) self.devices = {} - self.majorTypes = [ CMSG_NETIF_FE ] - - self.subTypes = { - CMSG_NETIF_FE_DRIVER_STATUS_CHANGED: - self.recv_fe_driver_status_changed, - CMSG_NETIF_FE_INTERFACE_CONNECT : - self.recv_fe_interface_connect, - } + self.addMethod(CMSG_NETIF_FE, + CMSG_NETIF_FE_DRIVER_STATUS_CHANGED, + self.recv_fe_driver_status_changed) + self.addMethod(CMSG_NETIF_FE, + CMSG_NETIF_FE_INTERFACE_CONNECT, + self.recv_fe_interface_connect) self.registerChannel() def sxpr(self): -- 2.30.2